import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';

import {
    Login,
    Home,
    HomeMain,
    MainDetail,
    MainArticle,
    MainList,
    MainSuccess,
    Sign,
    SignMain,
    CountList,
    MyCount
} from './pages';

Vue.use(VueRouter);

const routes = [
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/home',
        component:Home,
        name:'home',
        children: [
            {
                path:'',
                redirect:'main'
            },
            {
                path:'main',
                component:HomeMain,
                name:'homeMain'
            },
            {
                path:'main/detail',
                component:MainDetail,
                name:'mainDetail'
            },
            {
                path:'main/article',
                component: MainArticle,
                name:'MainArticle'
            },
            {
                path:'main/list',
                component: MainList,
                name:'MainList'
            },
            {
                path:'main/detail/success',
                component: MainSuccess,
                name:'MainSuccess'
            }
        ]
    },
    {
        path:'/sign',
        component:Sign,
        name:'sign',
        children: [
            {
                path:'',
                redirect:'main'
            },
            {
                path:'main',
                component:SignMain,
                name:'signMain'
            },
            {
                path:'list',
                component:CountList,
                name:'CountList'
            },
            {
                path:'mycount',
                component:MyCount,
                name:'MyCount'
            }
        ]
    }
];

export const router = new VueRouter({
    // mode: 'history',
   routes
});



router.beforeEach((to, from, next) => {
    Vue.prototype.$loading();
    console.log(to);
    // alert(JSON.stringify(to.query));
    // alert(window.location.href);
    if (!localStorage.id && !to.query.userId) {
        // console.log(to.fullPath);
        location.href = 'http://vw.hnwcl.com/public/integral/redirect?url=' + to.fullPath;
    } else if (!localStorage.id && to.query.userId) {
        localStorage.id = to.query.userId;
        // alert(localStorage.id);
    }
    next();
});
router.afterEach(route => {
    Vue.prototype.$loading.close();
});